﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarbonExchange.Bll;

namespace CarbonExchange.Tests
{
    [TestClass]
    public class CountryTests : BaseTest
    {

        [TestMethod]
        public void Country_GetAllBySiteId_WithIncorrectSiteId_ReturnEmptyList()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetAllBySiteId(int.MaxValue);
            Assert.AreEqual(0, tmp.Count());
        }

        [TestMethod]
        public void Country_GetAllBySiteId_WithCorrectSiteId_ReturnPopulatedList()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetAllBySiteId(_TestSite.Id);
            Assert.AreEqual(2, tmp.Count());
        }

        [TestMethod]
        public void Country_GetAllBySiteId_PopulatesCorrectProperties()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetAllBySiteId(_TestSite.Id).ToArray();
            Assert.AreEqual(2, tmp.Count());
            Assert.AreEqual("Switzerland", tmp[0].Name);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), tmp[0].Created);
            Assert.AreEqual(_TestSite.Id, tmp[0].SiteId);
        }

        [TestMethod]
        public void Country_GetById_WithIncorrectId_ReturnNull()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetById(int.MaxValue);
            Assert.IsNull(tmp);
        }

        [TestMethod]
        public void Country_GetById_WithCorrectId_ReturnNotNull()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetById(_TestCountries[0].Id);
            Assert.IsNotNull(tmp);
        }

        [TestMethod]
        public void Country_GetById_PopulatesCorrectProperties()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetById(_TestCountries[0].Id);
            Assert.AreEqual("United Kingdom", tmp.Name);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), tmp.Created);
            Assert.AreEqual(_TestSite.Id, tmp.SiteId);
        }

        [TestMethod]
        public void Country_DeleteOneTest()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetById(_TestCountries[0].Id);
            Assert.IsNotNull(tmp);
            factory.Delete(tmp);
            tmp = factory.GetById(_TestCountries[0].Id);
            Assert.IsNull(tmp);
            Assert.AreEqual(1, factory.GetAllBySiteId(_TestSite.Id).Count());
        }

        [TestMethod]
        public void Country_DeleteManyTest()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetAllBySiteId(_TestSite.Id);
            Assert.AreEqual(2, tmp.Count());
            factory.Delete(tmp);
            tmp = factory.GetAllBySiteId(_TestSite.Id);
            Assert.AreEqual(0, tmp.Count());
            Assert.IsNull(factory.GetById(_TestCountries[0].Id));
        }

        [TestMethod]
        public void Country_UpdateManyTest()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetAllBySiteId(_TestSite.Id).ToArray();
            for (int i = 0; i < tmp.Count(); i++)
            {
                tmp[i].Locale = string.Format("Update Test {0}", i);
            }
            factory.Save(tmp);
            for (int i = 0; i < tmp.Count(); i++)
            {
                Assert.AreEqual(string.Format("Update Test {0}", i), tmp[i].Locale);
            }

            tmp = factory.GetAllBySiteId(_TestSite.Id).ToArray();
            for (int i = 0; i < tmp.Count(); i++)
            {
                Assert.AreEqual(string.Format("Update Test {0}", i), tmp[i].Locale);
            }
        }

        [TestMethod]
        public void Country_UpdateOneTest()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = factory.GetById(_TestCountries[0].Id);
            Assert.IsNotNull(tmp);
            tmp.Locale = "Update Test";
            factory.Save(tmp);
            Assert.AreEqual("Update Test", tmp.Name);
            tmp = factory.GetById(_TestCountries[0].Id);
            Assert.AreEqual("Update Test", tmp.Name);
        }

        [TestMethod]
        public void Country_InsertOneTest()
        {
            CountryFactory factory = new CountryFactory(_Context);
            var tmp = new Country(_TestSite.Id);
            tmp.Locale = "Insert Test";
            DateTime expected = DateTime.Now;

            factory.Save(tmp);

            Assert.IsTrue(tmp.Id > 0);
            Assert.AreEqual("Insert Test", tmp.Name);
            Assert.AreEqual(_TestSite.Id, tmp.SiteId);
            Assert.IsTrue(tmp.Created - expected < TimeSpan.FromSeconds(1));

            var tmp2 = factory.GetById(tmp.Id);
            Assert.AreEqual(tmp.Id, tmp2.Id);
            Assert.AreEqual(tmp.SiteId, tmp2.SiteId);
            Assert.AreEqual(tmp.Created, tmp2.Created);
            Assert.AreEqual(tmp.Name, tmp2.Name);

        }

        [TestMethod]
        public void Country_InsertManyTest()
        {
            int itemsToInsert = 3;

            CountryFactory factory = new CountryFactory(_Context);
            List<Country> tmp = new List<Country>();
            for (int i = 0; i < itemsToInsert; i++)
            {
                tmp.Add(new Country(_AlternateSite.Id));
                tmp[i].Locale = string.Format("Insert Many {0}", i);
            }

            DateTime expected = DateTime.Now;

            factory.Save(tmp);

            for (int i = 0; i < itemsToInsert; i++)
            {
                Assert.IsTrue(tmp[i].Id > 0);
                Assert.AreEqual(string.Format("Insert Many {0}", i), tmp[i].Name);
                Assert.AreEqual(_AlternateSite.Id, tmp[i].SiteId);
                Assert.IsTrue(tmp[i].Created - expected < TimeSpan.FromSeconds(1));
            }
            var tmp2 = factory.GetAllBySiteId(_AlternateSite.Id).ToArray();
            Assert.AreEqual(itemsToInsert, tmp2.Count());
            for (int i = 0; i < itemsToInsert; i++)
            {
                Assert.AreEqual(tmp[i].Id, tmp2[i].Id);
                Assert.AreEqual(tmp[i].SiteId, tmp2[i].SiteId);
                Assert.AreEqual(tmp[i].Created, tmp2[i].Created);
                Assert.AreEqual(tmp[i].Name, tmp2[i].Name);
            }
        }

        /*[TestMethod]
        public void Country_SiteCountyListLoadsCorrectly()
        {
            SiteFactory siteFactory = new SiteFactory(_Context);
            var site = siteFactory.GetById(_TestSite.Id);
            var expectedCounty = _TestCountries.Where(x => x.SiteId == _TestSite.Id).OrderBy(x=>x.Name).Skip(1).First();
            Assert.AreEqual(_TestCountries.Where(x => x.SiteId == _TestSite.Id).Count(), site.Countries.Count());
            var tmp = site.Countries.Skip(1).Take(1).Single();
            Assert.AreEqual(expectedCounty.Id, tmp.Id);
            Assert.AreEqual(expectedCounty.Created.ToLocalTime(), tmp.Created);
            Assert.AreEqual(expectedCounty.Name, tmp.Name);
            Assert.AreEqual(expectedCounty.SiteId, tmp.SiteId);
        }*/
    }

}
